package com.wlz.thred;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 多线程
 *
 */
public class BioServerThread {
    public static void main(String[] args) {
        int port=8080;
        ServerSocket serverSocket=null;
        try{
            // 创建一个新的ServerSocket，绑定一个InetSocketAddress，监听8000端口上的连接请求
            serverSocket=new ServerSocket(port);
            Socket socket=null;
            // accept专门负责通信
            while (true){

                System.out.println("等待连接---");
                socket=serverSocket.accept();//拿到socket   10000
                System.out.println("连接成功---");

                // =====④:新建一个线程来处理这个客户端连接
                new Thread(new SocketHandler(socket)).start(); //1000
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                    serverSocket=null;
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
}
